package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.admin.req.user.TeamPerformancePageReq;
import com.letoken.platform.repo.admin.res.config.QueryLevelConfigRes;
import com.letoken.platform.repo.admin.res.user.TeamPerformancePageRes;
import com.letoken.platform.repo.app.po.AppEcoLevel;
import com.letoken.platform.repo.app.po.TeamPerformance;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * Le账户-生态建设等级 Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2022-12-13
 */
public interface TeamPerformanceMapper extends BaseMapper<TeamPerformance> {


    @Select("SELECT m.user_id userId,\n" +
            "       m.level\n" +
            "FROM app_user_team m\n" +
            "WHERE m.user_id IN (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(t.invite_path, ',', b.id + 1), ',', - 1) AS user_id\n" +
            "                    FROM app_user_team_path t\n" +
            "                             JOIN help_topic b\n" +
            "                                  ON b.id <= (LENGTH(t.invite_path) - LENGTH(REPLACE(t.invite_path, ',', '')))\n" +
            "                    WHERE t.user_id = #{userId}\n" +
            "                    ORDER BY NULL)\n" +
            "  and level = 2\n" +
            "GROUP BY m.LEVEL\n" +
            "ORDER BY NULL limit 1")
    Integer selectRecentLeader(@Param("userId") Integer userId);


    @Select({"<script>",
            "select aut.user_id leader,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 1 then performance end),0)  fund,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 2 then performance end),0)  minting,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 3 then performance end),0)  consumption,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 4 then performance end),0)  usdtRecharge,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 5 then performance end),0)  usdtWithdraw,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 6 then performance end),0)  usdtInternalTransfer,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 7 then performance end),0)  usdtInternalTransferOut,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 8 then performance end),0)  capRecharge,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 9 then performance end),0)  capWithdraw,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 10 then performance end),0) capInternalTransfer,\n" +
                    "       ifnull(sum(case\n" +
                    "               when type = 11 then performance end),0) capInternalTransferOut,\n" +
                    "aut.level grade \n" +
                    "from app_user_team aut left join team_performance  tp on aut.user_id = tp.recent_leader     \n" +
                    "<if test='startTime != null'> " +
                    "  and   <![CDATA[ tp.created_at >= #{startTime} ]]> " +
                    "</if> " +
                    "<if test='endTime != null'> " +
                    "  and  <![CDATA[ tp.created_at <= #{endTime} ]]>  " +
                    "</if> " +
                    "where aut.level = 2" +
                    "<if test='userId != null'> " +
                    "  and aut.user_id = #{userId} " +
                    "</if> " +

                    "group by aut.user_id",
            "</script>"})
    Page<TeamPerformancePageRes> page(@Param("page") Page<?> page, @Param("userId") Integer userId, @Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);

}
